package cn.yunhe.service;

import cn.yunhe.dao.HouseMapper;
import cn.yunhe.dao.NewsMapper;
import cn.yunhe.entity.House;
import cn.yunhe.entity.Page;
import cn.yunhe.entity.Users;
import cn.yunhe.util.PageUtil;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class UserService {
    SqlSession session = SqlSessionFactoryUtil.getSqlSession();
    NewsMapper newsMapper = session.getMapper(NewsMapper.class);
    HouseMapper  houseMapper = session.getMapper(HouseMapper.class);
    public int insert(Users users){
        int flag=newsMapper.insert(users);
        session.commit();
        return  flag;

    }
    public Users getNewsByUp(Users users){
        return   newsMapper.getNewsByUp(users);
    }
    public int addHouse(House house){
        int flag = houseMapper.addHouse(house);
        session.commit();
        return  flag;
    }
    public List<House> news(int pageIndex,String keyword){
        Page page = new Page();
        page.setKeyword(keyword);
        page.setPageIndex(PageUtil.PAGE_SIZE*(pageIndex-1));
        page.setPageSize(PageUtil.PAGE_SIZE);
        return  houseMapper.news(page);
    }
     public  int  getTotalPages(){
        int count = houseMapper.getcounts();
        return  PageUtil.getTotalPages(PageUtil.PAGE_SIZE,count);
     }
     public House newsById(int id){
        return   houseMapper.newsById(id);
     }

}
